"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】执行触发table_rewrite事件触发器，事件在表重写之前触发
Create At   :   2024/07/20
Owner       :   sungang14
Description :
    预置条件：
        1.创建事件触发器函数：
        CREATE OR REPLACE FUNCTION f_eventtrigger_0072()
        RETURNS event_trigger
        AS $$
        DECLARE
            obj record;
        BEGIN
            FOR obj IN SELECT * FROM pg_event_trigger_ddl_commands()
                LOOP
                RAISE NOTICE 'command: %',
                        obj.command_tag;
                    RAISE NOTICE 'triggered';
                END LOOP;
        END; $$ LANGUAGE plpgsql;
    测试步骤：
        1.创建事件触发器
        CREATE EVENT TRIGGER eventtri_eventtrigger_0072 ON ddl_command_end EXECUTE PROCEDURE f_eventtrigger_0072();
        2、创建表
        create table t_eventtrigger_0072(id int);
        3.修改表
        ALTER TABLE t_eventtrigger_0072 alter column id type numeric;
        4、删除表
        drop table t_eventtrigger_0072;
Expect      :
        1.创建事件触发器成功
        2.创建表成功
        NOTICE：command:CREATE TABLE
        NOTICE: triggered
        3、修改表触发执行函数f_eventtrigger_0072()  注：当前B库有问题不触发事件触发器函数，已有问题单跟踪
        NOTICE: commadn:ALTER TABLE
        NOTICE: triggered
        ALTER TABLE
        4.删除表不触发执行函数f_eventtrigger_0072()
        DROP TABLE
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.comsh = CommonSH('PrimaryDbBmode')
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0072;")
        self.comsh.execut_db_sql("drop TABLE if exists t_eventtrigger_0072;")
        self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0072();")
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION f_eventtrigger_0072() RETURNS event_trigger LANGUAGE plpgsql" \
            f" AS \$\$ " \
            f"DECLARE " \
            f" obj record; " \
            f"BEGIN " \
            f" FOR obj IN SELECT * FROM pg_event_trigger_ddl_commands() " \
            f" LOOP " \
            f" RAISE NOTICE 'command: %', " \
            f" obj.command_tag; " \
            f" RAISE NOTICE 'triggered'; " \
            f"END LOOP;" \
            f"END;" \
            f"\$\$;")
        self.logger.info(msg)
        self.assertIn("CREATE FUNCTION", msg)

    def test_Index_file_damaged(self):
        self.logger.info("--step1:创建事件触发器")
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER eventtri_eventtrigger_0072 ON table_rewrite EXECUTE PROCEDURE f_eventtrigger_0072();")
        self.logger.info(msg)
        self.assertEqual(f'CREATE EVENT TRIGGER', msg)
        self.logger.info("--step2:创建表成功")
        msg = self.comsh.execut_db_sql(f"create table t_eventtrigger_0072(id int);")
        self.logger.info(msg)
        self.assertIn(f"NOTICE:  command: CREATE TABLE" and \
                      f"NOTICE:  triggered" and \
                      f"CREATE TABLE", msg)
        self.logger.info("--step3:修改表触发执行函数f_eventtrigger_0072()")
        msg = self.comsh.execut_db_sql("ALTER TABLE t_eventtrigger_0072 alter column id type numeric;")
        self.logger.info(msg)
        self.assertIn(f'ALTER TABLE', msg)
        self.logger.info("--step4:删除表")
        msg = self.comsh.execut_db_sql(f"drop table t_eventtrigger_0072;")
        self.logger.info(msg)
        self.assertIn("DROP TABLE", msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0072;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0072();")
        self.logger.info(msg)
        self.assertIn('DROP FUNCTION', msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
